package com.example.lenovo.litepaltest;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;

import org.litepal.LitePal;
import org.litepal.crud.DataSupport;

import java.util.List;


public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getCanonicalName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 创建数据库
        Button createDatabase = findViewById(R.id.btn_createDb);
        createDatabase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LitePal.getDatabase();
            }
        });
        // 插入数据
        Button insertData = findViewById(R.id.btn_insertData);
        insertData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book = new Book();
                book.setAuthor("ZHT");
                book.setId(1);
                book.setName("JAVA PROGRAMMING");
                book.setPages(500);
                book.setPrice(59.00);
                book.save();
            }
        });
        Button updateData = findViewById(R.id.btn_updateData);
        // 一般数据更新(更新某列数据)
        updateData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book = new Book();
                book.setPrice(69.00);
                book.save();
            }
        });
        // 更新指定条件的数据
        updateData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Book book = new Book();
            // 要更新的列表信息
            book.setPrice(49.00);
            book.setAuthor("zht");
            // 如果不指定条件约束，则更新所有数据，这里表示更新所有书价是 69.00 元且页数是 500 的书价和作者
            book.updateAll("price = ? and pages = ", "69.00", "500");
            // 将所有 price 的值改为 30
            book.updateAll("price", "30");
            }
        });
        Button deleteData = findViewById(R.id.btn_deleteData);
        // 一般数据的删除(删除所有数据)
        deleteData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataSupport.deleteAll(Book.class);
            }
        });
        // 删除指定条件的数据
        deleteData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataSupport.deleteAll(Book.class, "price > ?", "40");
            }
        });
        Button queryData = findViewById(R.id.btn_queryData);
        // 一般查询所有数据
        queryData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Book> bookList = DataSupport.findAll(Book.class);
                for (Book book : bookList) {
                    Log.d(TAG, "book name is: " + book.getName());
                    Log.d(TAG, "book author is: " + book.getAuthor());
                    Log.d(TAG, "book price is: " + book.getPrice());
                    Log.d(TAG, "book pages is: " + book.getPages());
                }
            }
        });
        // 复杂查询操作
        queryData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Book> bookList = DataSupport.select("name", "author", "pages")
                    .where("pages > ?", "400").order("pages").limit(0).offset(1).find(Book.class);
                for (Book book : bookList) {
                    Log.d(TAG, "book name is: " + book.getName());
                    Log.d(TAG, "book author is: " + book.getAuthor());
                    Log.d(TAG, "book price is: " + book.getPrice());
                    Log.d(TAG, "book page is: " + book.getPages());
                }
            }
        });
        // 原生 SQL 语句查询
        queryData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Cursor cursor = DataSupport.findBySQL("select * from Book where pages > ? and price > ?", "400", "50");
                if (cursor.moveToFirst()) {
                    do {
                        String name = cursor.getString(cursor.getColumnIndex("name"));
                        String author = cursor.getString(cursor.getColumnIndex("author"));
                        double price = cursor.getDouble(cursor.getColumnIndex("price"));
                        int pages = cursor.getInt(cursor.getColumnIndex("pages"));
                        Log.d(TAG, "book name is: " + name);
                        Log.d(TAG, "book author is: " + author);
                        Log.d(TAG, "book price is: " + price);
                        Log.d(TAG, "book page is: " + pages);
                    } while (cursor.moveToNext());
                }
                cursor.close();
            }
        });
    }
}
